<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<meta charset="utf-8">
<head>
    <th:block th:include="include :: header('我的任务列表')"/>
    <th:block th:include="include :: datetimepicker-css"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="config-form">
                <div class="select-list">
                    <ul>
                        <li>
                            流程定义key名称：
                            <select name="procDefKey" th:with="type=${@dict.getType('act_key')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            创建时间起：<input id="startTime" type="text" name="startTime" placeholder="yyyy-MM-dd HH:mm:ss"/>
                        </li>
                        <li>
                            创建时间止：<input id="endTime" type="text" name="endTime" placeholder="yyyy-MM-dd HH:mm:ss"/>
                        </li>
                        <li>
                            <a class="btn btn-success btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-success btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<div th:include="include :: footer"></div>
<th:block th:include="include :: datetimepicker-js"/>
<th:block th:include="include :: select2-js"/>
<script th:inline="javascript">
    var removeFlag = [[${@permission.hasPermi('activiti:leave:remove')}]];
    var listFlag = [[${@permission.hasPermi('activiti:leave:list')}]];
    var prefix = ctx + "activiti/task";
    var datas_act_key = [[${@dict.getType('act_key')}]];
    var datas_act_category = [[${@dict.getType('act_category')}]];
    $(function () {
        init()
        $("#startTime").datetimepicker({
            format: "yyyy-mm-dd",
            autoclose: true,//选中后自动关闭
            clearBtn: true, //显示清除按钮
            minView: "month",
            todayBtn: true  //显示今天按钮
        }).on('changeDate', function (event) {
            event.preventDefault();
            event.stopPropagation();
            var startTime = event.date;
            $('#endTime').datetimepicker('setStartDate', startTime);
        });

        $("#endTime").datetimepicker({
            format: "yyyy-mm-dd",
            autoclose: true,//选中后自动关闭
            clearBtn: true, //显示清除按钮
            minView: "month",
            todayBtn: true  //显示今天按钮
        }).on('changeDate', function (event) {
            event.preventDefault();
            event.stopPropagation();
            var endTime = event.date;
            $("#startTime").datetimepicker('setEndDate', endTime);
        });
    });

    function init() {
        var options = {
            uniqueId: "id",
            url: prefix + "/todoList",
            removeUrl: prefix + "/remove?taskId={taskId}",
            modalName: "我的任务",
            search: false,
            showExport: false,
            columns: [{
                checkbox: true
            },
                {
                    field: 'task.id',
                    title: '任务ID',
                    visible: false
                }, {
                    title: '当前待执行环节',
                    formatter: function (value, row, index) {
                        var action = '<a href="#" onclick="taskTracking(\'' + row.task.processDefinitionId + '\',\'' + row.task.processInstanceId + '\')">' + row.task.name + '</a>';
                        return action;
                    }
                },
                {
                    field: 'vars.applyUserId',
                    title: '任务申请人登录名称',
                    visible: false
                },
                {
                    field: 'vars.applyUserName',
                    title: '任务申请人用户名'

                },
                {
                    title: '任务内容',
                    formatter: function (value, row, index) {
                        var action = "";
                        if (row.task.assignee == null) {
                            action = '<a href="#" th:title="签收任务">' + row.vars.title + '</a>';
                        } else {
                            action = '<a href="#" onclick="taskTodo(\'' + row.task.id + '\',\'' + row.task.name + '\',\'' + row.task.taskDefinitionKey + '\',\'' + row.task.processDefinitionId + '\',\'' + row.task.processInstanceId + '\',\'' + row.status + '\')">' + row.vars.title + '</a>';
                        }
                        return action;
                    }
                },
                {
                    field: 'task.processDefinitionId',
                    title: '流程定义ID',
                    visbale: false

                },
                {
                    field: 'task.processInstanceId',
                    title: '流程实例ID',
                    visible: true
                },
                {
                    field: 'procDef.key',
                    title: '流程定义key'
                },

                {
                    field: 'procDef.name',
                    title: '流程定义名称'
                },

                {
                    field: 'procDef.category',
                    title: '流程分类',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(datas_act_category, value);
                    },
                },

                {
                    field: 'procDef.key',
                    title: '流程定义key名称',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(datas_act_key, value);
                    }
                },
                {
                    field: 'procDef.version',
                    title: '流程版本'
                },
                {
                    field: 'task.createTime',
                    title: '创建时间'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        if (row.task.assignee == null) {
                            actions.push('<a class="btn btn-success btn-xs" href="#" onclick="taskSign(\'' + row.task.id + '\')"><i class="fa fa-play-circle-o"></i>任务签收</a>');
                        } else {
                            actions.push('<a class="btn btn-success btn-xs" href="#" onclick="taskTodo(\'' + row.task.id + '\',\'' + row.task.name + '\',\'' + row.task.taskDefinitionKey + '\',\'' + row.task.processDefinitionId + '\',\'' + row.task.processInstanceId + '\',\'' + row.status + '\')"><i class="fa fa-play-circle-o"></i>任务办理</a> ');
                        }

                        if (row.task.executionId == null) {
                            actions.push('<a class="btn btn-success btn-xs" href="#" onclick="$.operate.remove(\'' + row.task.id + '\')"><i class="fa fa-remove"></i>删除任务</a> ');
                        }
                        actions.push('<a class="btn btn-success btn-xs" href="#" onclick="taskTracking(\'' + row.task.processDefinitionId + '\',\'' + row.task.processInstanceId + '\')"><i class="fa fa-play-circle-o"></i>跟踪</a>');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    }

    //任务签收
    function taskSign(taskId) {
        var url = prefix + "/claim?taskId=" + taskId;
        $.modal.confirm("确定签收？", function () {
            $.operate.get(url);
            // 刷新表格内容
            $.refresh("#bootstrap-table");
        });

    }

    //任务办理
    function taskTodo(taskId, taskName, taskDefKey, procDefId, procInsId, status) {
        var url = encodeURI(prefix + "/form?taskId=" + taskId + "&taskName=" + taskName + "&taskDefKey=" + taskDefKey + "&procDefId=" + procDefId + "&procInsId=" + procInsId + "&status=" + status);
        $.modal.openFullS('任务办理', url);
    }

    //任务跟踪
    function taskTracking(processDefinitionId, processInstanceId) {
        var url = ctx + "diagram-viewer/index.html?processDefinitionId=" + processDefinitionId + "&processInstanceId=" + processInstanceId;
        var options = {
            title: '任务跟踪',
            width: 1050,
            url: url,
            btn: ['关闭'],
            yes: function (index) {
                $.modal.close(index);
            }
        };
        $.modal.openOptions(options);
    }

</script>
</body>
</html>